package fichier;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class Modele {

	private int ID;
	private Box emplacement;
	private Image image;
	private Client client;
	private String designation;
	private String numero_modele;
	private String numero_plan;
	private String commentaire;
	private int nombre_moule;
	private int nombre_boite;
	private int anneeCreation;
	private static final String table = "modele";
    public static final ArrayList < Modele > modeleList = new ArrayList < Modele >();
	
	public Modele(int iD){
		super();
		SelectData.REQUETE = "select designation_modele, " +
				"id_client, " +
				"numero_modele, " +
				"numero_plan_modele, " +
				"annee_modele, " +
				"id_box, " +
				"commentaire_modele, " +
				"nombre_moule_modele, " +
				"nombre_boite_modele " +
			"from modele m " +
			"where m.id_modele = " + iD;
		ResultSet infoModele = SelectData.startSelect();
		this.ID = iD;
		int iDBox;
		int iDClient;

		try {
			if(infoModele.next()){
				this.designation = infoModele.getString("designation_modele");
				this.numero_modele = infoModele.getString("numero_modele");
				this.numero_plan = infoModele.getString("numero_plan_modele");
				this.commentaire = infoModele.getString("commentaire_modele");
				this.nombre_moule = Integer.parseInt(infoModele.getString("nombre_moule_modele"));
				this.nombre_boite = Integer.parseInt(infoModele.getString("nombre_boite_modele"));
				this.anneeCreation = Integer.parseInt(infoModele.getString("annee_modele"));
				iDBox = Integer.parseInt(infoModele.getString("id_box"));
				iDClient = Integer.parseInt(infoModele.getString("id_client"));
				this.emplacement = new Box(iDBox);
				this.client = new Client(iDClient);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}
    
    public Modele(int iD, Box emplacement, Client client, Image image, String designation,
			String numero_modele, String numero_plan, String commentaire,
			int nombre_moule, int nombre_boite, int anneeCreation) {
		super();
		ID = iD;
		this.emplacement = emplacement;
		this.client = client;
		this.image = image;
		this.designation = SQLadaptString.adapt(designation);
		this.numero_modele = SQLadaptString.adapt(numero_modele);
		this.numero_plan = SQLadaptString.adapt(numero_plan);
		this.commentaire = SQLadaptString.adapt(commentaire);
		this.nombre_moule = nombre_moule;
		this.nombre_boite = nombre_boite;
		this.anneeCreation = anneeCreation;
		Modele.modeleList.add(this);
	}


	public Modele(int id_box, int id_client, int id_image, String designation, String numero_modele,
			String numero_plan, String commentaire, int nombre_moule, int nombre_boite, int anneeCreation){
		super();
		
		this.designation = SQLadaptString.adapt(designation);
		this.numero_modele = SQLadaptString.adapt(numero_modele);
		this.numero_plan = SQLadaptString.adapt(numero_plan);
		this.commentaire = SQLadaptString.adapt(commentaire);
		this.nombre_moule = nombre_moule;
		this.nombre_boite = nombre_boite;
		this.anneeCreation = anneeCreation;
		
		
		if(this.emplacement==null)
		{
			SelectData.REQUETE="SELECT nom_box FROM box WHERE id_box="+id_box;
			ResultSet resultbox = SelectData.startSelect();
			try {
				while(resultbox.next()){
					String test = resultbox.getString("nom_box");
					this.setEmplacement(new Box(id_box,test));
				}
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		for(Client clientTest : Client.clientList)
		{
			if(clientTest.getID()==id_box)
			{
				this.setClient(clientTest);
			}
				
		}
		if(this.client==null)
		{
			SelectData.REQUETE="SELECT * FROM client WHERE id_client="+id_client;
			ResultSet resultclient = SelectData.startSelect();
			try {
				while(resultclient.next()){
					String nom = resultclient.getString("nom_client");
					String adresse1 = resultclient.getString("adresse1_client");
					String adresse2 = resultclient.getString("adresse2_client");
					String CP = resultclient.getString("cp_client");
					String ville = resultclient.getString("ville_client");
					this.setClient(new Client(id_client,nom,adresse1,adresse2,CP,ville));
				}
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(id_image!=0)
		{
			for(Image imageTest : Image.imageList)
			{
				if(imageTest.getID()==id_image)
				{
					this.setImage(imageTest);
				}
					
			}
			if(this.image.getID()==id_image)
			{
				SelectData.REQUETE="SELECT fichier_image FROM image WHERE id_image="+id_image;
				ResultSet resultbox = SelectData.startSelect();
				try {
					while(resultbox.next()){
						String test = resultbox.getString("fichier_image");
						this.setImage(new Image(id_image,new File(test)));
					}
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		else this.image = new Image(0);
		this.ajouter();
		Modele.modeleList.add(this);
	}
	
	private void ajouter(){
		SelectData.REQUETE  = "INSERT INTO "+ table +" VALUES (null,"+ this.emplacement.getID()
				+", "+ this.client.getID()+", ";
		if(this.image.getID()==0)
			SelectData.REQUETE=SelectData.REQUETE + "null";
		else
			SelectData.REQUETE=SelectData.REQUETE + this.image.getID();
		
		SelectData.REQUETE=SelectData.REQUETE +", '" +this.designation+"', '" +this.numero_modele+"', '"+this.numero_plan+"', '"
				+this.commentaire+"', "+this.nombre_moule+", "+this.nombre_boite+ ", " + this.anneeCreation + ")";
		//System.out.println(SelectData.REQUETE);
		SelectData.startUpdate();
		SelectData.REQUETE = "SELECT MAX(id_modele) as 'test' FROM "+ table;
		ResultSet result = SelectData.startSelect();
		String s;
		try {
			while(result.next()){
				s = result.getString("test");
				int lastID = Integer.parseInt( s );
				this.setID(lastID);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void update(){
		SelectData.REQUETE  = "UPDATE "+ table +" SET " +
							"id_box="+ this.emplacement.getID() +"," +
							"id_client="+ this.client.getID() +"," +
							"designation_modele= '"+this.getDesignation()+"'," +
							"numero_modele= '"+this.getNumero_modele()+"'," +
							"numero_plan_modele= '"+this.getNumero_plan()+"'," +
							"commentaire_modele= '"+this.getCommentaire()+"'," +
							"nombre_moule_modele= "+this.getNombre_moule()+"," +
							"nombre_boite_modele= "+this.getNombre_boite()+"," +
							"annee_modele= "+this.getAnneeCreation()+" " +
							"WHERE " +
							"id_modele="+this.getID();
		//System.out.println(SelectData.REQUETE);
		SelectData.startUpdate();
	}
	
	public void supprimer(){
		SelectData.REQUETE = "DELETE FROM "+ table +" WHERE id_modele ="+this.getID();
		//System.out.println(SelectData.REQUETE);
		SelectData.startUpdate();
	}
	
	public static void supprimer(int iD){
		SelectData.REQUETE = "DELETE FROM "+ table +" WHERE id_modele ="+iD;
		//System.out.println(SelectData.REQUETE);
		SelectData.startUpdate();
	}
	
	
	// GETTERS
	
	public int getID() {
		return ID;
	}
	public Box getEmplacement() {
		return emplacement;
	}
	public Client getClient() {
		return client;
	}
	public String getDesignation() {
		return designation;
	}
	public String getNumero_modele() {
		return numero_modele;
	}
	public String getNumero_plan() {
		return numero_plan;
	}
	public String getCommentaire() {
		return commentaire;
	}
	public int getNombre_moule() {
		return nombre_moule;
	}
	public int getNombre_boite() {
		return nombre_boite;
	}
	public int getAnneeCreation() {
		return anneeCreation;
	}
	
	// SETTERS
	
	public void setID(int id_modele) {
		this.ID = id_modele;
	}
	public void setEmplacement(Box emplacement) {
		this.emplacement = emplacement;
	}
	public void setClient(Client client) {
		this.client = client;
	}
	public void setDesignation(String designation) {
		this.designation = designation;
	}
	public void setNumero_modele(String numero_modele) {
		this.numero_modele = numero_modele;
	}
	public void setNumero_plan(String numero_plan) {
		this.numero_plan = numero_plan;
	}
	public void setCommentaire(String commentaire) {
		this.commentaire = commentaire;
	}
	public void setNombre_moule(int nombre_moule) {
		this.nombre_moule = nombre_moule;
	}
	public void setNombre_boite(int nombre_boite) {
		this.nombre_boite = nombre_boite;
	}
	public void setAnneeCreation(int anneeCreation) {
		this.anneeCreation = anneeCreation;
	}


	public Image getImage() {
		return image;
	}


	public void setImage(Image image) {
		this.image = image;
	}
	
	
}